import re
import json
import pandas


def process_eams(session):
    session.get('https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/login?service=http://jxglstu.hfut.edu.cn/eams5-student/neusoft-sso/login')
    data = dict()
    studentId = re.findall('/(\d+)$', session.get(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421faef469034247d1e760e9cb8d6502720ede479/eams5-student/for-std/student-info').history[0].headers['Location'])[0]
    semesterId, semesterName = re.findall('value="(\d+)">(.+?)<', session.get(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421faef469034247d1e760e9cb8d6502720ede479/eams5-student/for-std/course-table').text)[0]
    data['info'] = json.loads(session.get(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421faef469034247d1e760e9cb8d6502720ede479/eams5-student/ws/student/home-page/students?vpn-12-o1-jxglstu.hfut.edu.cn').text)
    data['grade'] = pandas.read_html(session.get(
        f'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421faef469034247d1e760e9cb8d6502720ede479/eams5-student/for-std/grade/sheet/info/{studentId}?vpn-12-o1-jxglstu.hfut.edu.cn&semester=').text)[0].to_dict(orient='index')
    data['course'] = json.loads(session.get(
        f'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421faef469034247d1e760e9cb8d6502720ede479/eams5-student/for-std/course-table/get-data?vpn-12-o1-jxglstu.hfut.edu.cn&bizTypeId=2&semesterId={semesterId}&dataId={studentId}').text)
    data['semester'] = semesterName
    return data


def process_card(session):
    data = dict()
    session.get('https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421f3f652d22f367d44300d8db9d6562d/cas/login?service=http://172.31.248.20/ahdxdrPortalHome.action')
    userInfo = pandas.read_html(session.get(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421a1a013d2746126022a50c7fec8/accountcardUser.action').text)
    account = re.findall(
        '：(\d+?)性别', userInfo[0].to_dict()[0][0].replace(' ', ''))[0]
    session.get(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421a1a013d2746126022a50c7fec8/accounthisTrjn.action')
    session.post('https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421a1a013d2746126022a50c7fec8/accounthisTrjn1.action',
                 data={'account': account, 'inputObject': 'all', 'Submit': r'+%C8%B7+%B6%A8+'})
    session.post('https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421a1a013d2746126022a50c7fec8/accounthisTrjn2.action',
                 data={'inputStartDate': '20200101', 'inputEndDate': '20201231'})
    record = pandas.read_html(session.post(
        'https://webvpn.hfut.edu.cn/http/77726476706e69737468656265737421a1a013d2746126022a50c7fec8/accounthisTrjn3.action').text)[3].to_dict(orient='index')
    data['total'] = re.findall('为:(.+?)（元）', record[18][0].replace(' ', ''))[0]
    return data


def get_data_card(session):
    data = dict()
    data = process_card(session)
    return json.dumps(data, ensure_ascii=False)


def get_data_eams(session):
    data = dict()
    data = process_eams(session)
    return json.dumps(data, ensure_ascii=False)


def get_data(session):
    data = dict()
    data['eams'] = process_eams(session)
    data['card'] = process_card(session)
    return json.dumps(data, ensure_ascii=False)
